<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ～ ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\storehouse\excl;

use catchAdmin\basics\model\Provider;
use catchAdmin\product\model\Product;
use catchAdmin\stock\model\Pick;
use catchAdmin\storehouse\model\WarehouseOut;
use catchAdmin\storehouse\model\WarehouseOutSon;
use catcher\CatchAuth;
use catcher\library\excel\ExcelContract;
use PhpOffice\PhpSpreadsheet\Style\Alignment;

class warehouseOutExport implements ExcelContract
{
    protected $model;


    public function __construct(WarehouseOut $WarehouseOutModel)
    {
        $this->model = $WarehouseOutModel;
    }

    /**
     * 设置头部
     *
     * @time 2020年09月08日
     * @return string[]
     */
    public function headers(): array
    {
        // TODO: Implement headers() method.
        return [
            '出库单号',
            '收货人',
            '地址',
            '型号',
            '尺寸',
            '出库数量',
            'SN码'
        ];
    }

    /**
     * 处理数据
     *
     * @time 2020年09月08日
     * @return \think\Collection
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     */
    public function sheets()
    {
        $post = request()->post();
        $user = (new CatchAuth())->user();
        $user_id = $user->id;
        $tenant_id = $user['tenant_id'];
//        $outOrderIdArr = [];
//        if (!empty($post['ids'])) {
//            $outOrderIdArr = explode(',', $post['ids']);
//        }
        $outOrderIdArr = (new WarehouseOut())
            ->where(function ($query) use ($post) {
                if (!empty($post['ids']))
                    $query->where('id','in',$post['ids']);
                if (!empty($post['project_name'])) {
                    $query->where('project_name', 'like', '%' . $post['project_name'] . '%');
                }
                if (!empty($post['ship_to'])) {
                    $query->where('ship_to', 'like', '%' . $post['ship_to'] . '%');
                }
                if (!empty($post['warehouse_out_id'])) {
                    $query->where('warehouse_out_id', $post['warehouse_out_id']);
                }
                if (!empty($post['created_at'])) {
                    $query->whereBetweenTime('created_at', $post['created_at'][0], $post['created_at'][1]);
                }

            })->column('warehouse_out_id');
        $pickList = (new Pick())->where([
            'status' => 1,
            'deleted_at' => 0,
            'tenant_id' => $tenant_id
        ])->where(function ($query) use ($outOrderIdArr, $post) {
            if (!empty($outOrderIdArr))
                $query->whereIn('warehouse_out_id', $outOrderIdArr);
        })->select();
        $data = [];
        foreach ($pickList as $gVal) {
            $outInfo = (new WarehouseOut())->where([
                'warehouse_out_id' => $gVal['warehouse_out_id'],
                'deleted_at' => 0,
            ])->find();
            $outSonInfo = (new WarehouseOutSon())->where([
                'warehouse_out_id' => $gVal['warehouse_out_id'],
                'warehouse_out_son_id' => $gVal['warehouse_out_son_id'],
                'deleted_at' => 0
            ])->find();
            $productInfo = (new Product())->where([
                'goods_id' => $outSonInfo['goods_id'],
                'deleted_at' => 0
            ])->find();
            if (!empty($outInfo) && !empty($productInfo) && !empty($productInfo)) {
                $data[] = [
                    $outInfo['warehouse_out_id'],
                    $outInfo['ship_to_name'],
                    $outInfo['ship_to_address'],
                    $productInfo['sku'],
                    $productInfo['new_carton_size'],
                    $outInfo['actual_number'],
                    $gVal['sn']
                ];
            }
        }
        return $data;
    }

    /**
     * 设置开始行
     *
     * @time 2020年09月08日
     * @return int
     */
    public function setRow()
    {
        return 2;
    }

    /**
     * 设置行宽
     *
     * @time 2020年09月08日
     * @return int
     */
    public function setWidth()
    {
        return [
            "A" => 30,
            "B" => 20,
            "C" => 20,
            "D" => 30,
            "E" => 30,
            "F" => 20,
            "G" => 20,
            "H" => 20,
            "I" => 20,
            "J" => 20,
            "K" => 20,
            "L" => 20,
//            "M"=>20,
//            "N"=>20,
//            "O"=>20,
//            "P"=>20,
//            "Q"=>20,
//            "R"=>20,
//            "S"=>20,
//            "T"=>30,
//            "U"=>30,
//            "V"=>30,
//            "W"=>30,
//            "X"=>30,
//            "Y"=>30,
//            "Z"=>20,
//            "AA"=>20,
//            "AB"=>20
        ];
    }

    /**
     * 设置标题
     *
     * @time 2020年09月08日
     * @return array
     */
    public function setTitle()
    {
        return [
            'A1:J1', '出库单', Alignment::HORIZONTAL_CENTER
        ];
    }
}